package com.xujl.hsns.codec.impl;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.*;
import com.dyuproject.protostuff.JsonIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.xujl.hsns.carriers.Invoke;
import com.xujl.hsns.carriers.Result;

public class JSONEncoder implements ProtocolEncoder {
	private final static Logger LOGGER = LoggerFactory.getLogger(JSONEncoder.class);
	private Schema invokeSchema;
	private Schema resultSchema;
	SerializeConfig serializeConfig = new SerializeConfig();
	PropertyFilter filter = new PropertyFilter() {
	    public boolean apply(Object source, String name, Object value) {
	    	if(source.getClass().toString().matches(".*org.hibernate.*")||name.matches("(beanName)|(className)|(hibernateLazyInitializer)"))return false;
	    	//System.err.println(source.getClass()+"-name: "+name+" value: "+value);
	        return true;
	    }
	};
	public JSONEncoder(){
		invokeSchema= RuntimeSchema.createFrom(Invoke.class);
		resultSchema =RuntimeSchema.createFrom(Result.class);
	}
	public void dispose(IoSession session) throws Exception {
		// TODO Auto-generated method stub

	}
	
	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws Exception {
		SerializeWriter writer = new SerializeWriter();
		
		JSONSerializer serializer = new JSONSerializer(writer,serializeConfig);
		
		serializer.getPropertyFilters().add(filter);
		serializer.config( SerializerFeature.PrettyFormat, false);
		serializer.config( SerializerFeature.WriteClassName, true);
		//serializer.config( SerializerFeature.QuoteFieldNames , false);
		byte[] bytes = JSON.toJSONBytes(message, SerializerFeature.WriteClassName);
		//System.out.println(new String(bytes));
		serializer.write(message);
		
		//byte[] bytes=writer.toBytes();
		//System.out.println(new String(bytes));
		LOGGER.warn(message.getClass()+" length:"+bytes.length);
		LOGGER.debug(message.getClass()+" length:"+bytes.length);
		IoBuffer buf = IoBuffer.allocate(bytes.length+4, false); 
		buf.putInt(bytes.length); 
		buf.put(bytes);
		buf.flip(); 
		
		out.write(buf); 
		//LOGGER.warn( buf.array().length+"");
		
	}

/*	public void encode(IoSession session, Object message,
			ProtocolEncoderOutput out) throws Exception {
			byte[] bytes = JsonIOUtil.toByteArray(message,  Invoke.class == message.getClass() ? invokeSchema :resultSchema , false);
			session.getTransportMetadata().hasFragmentation();
			LOGGER.warn(message.getClass()+" length:"+bytes.length);
			LOGGER.debug(message.getClass()+" length:"+bytes.length);
	        IoBuffer buf = IoBuffer.allocate(bytes.length+4, false); 
	        buf.putInt(bytes.length); 
	        buf.put(bytes);
	        buf.flip(); 
	        
	        out.write(buf); 
	        LOGGER.warn( buf.array().length+"");

	}*/

}
